package com.tingyuge.base.jdk.io.nio.asyncfilechannel;

import org.junit.Test;

import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2022/1/12
 * @description
 */
public class AsyncFileChannelDemo {

    @Test
    public void writeAsyncFileCompletionFuture() throws IOException {
        //1.创建异步文件对象
        Path path = Paths.get("D:/nio/002.txt");
        AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
        //2.创建buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("i'm write".getBytes());
        buffer.flip();
        asynchronousFileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(Integer result, ByteBuffer attachment) {
                System.out.println("写入完成");
            }

            @Override
            public void failed(Throwable exc, ByteBuffer attachment) {

            }
        });
    }

    @Test
    public void writeAsyncFileFuture() throws IOException {
        //1.创建异步文件对象
        Path path = Paths.get("D:/nio/002.txt");
        AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
        //2.创建buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("i'm write".getBytes());
        buffer.flip();
        Future<Integer> future = asynchronousFileChannel.write(buffer, 0);
        while (!future.isDone());
        System.out.println("write over");
        buffer.clear();
    }



    @Test
    public void testReadFileChannelCompletionFuture() throws IOException {
        //1.创建异步文件对象
        Path path = Paths.get("D:/nio/002.txt");
        AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        //2.创建buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //3.调用 channel 的 read方法得到future
       asynchronousFileChannel.read(buffer, 0, buffer,new CompletionHandler<Integer,ByteBuffer>() {


           @Override
           public void completed(Integer result, ByteBuffer attachment) {
               System.out.println("result: "+result);
               attachment.flip();
               byte [] data = new byte [attachment.limit()];
               attachment.get(data);
               System. out .println( new String(data));
               attachment.clear();
           }

            @Override
            public void failed(Throwable exc, ByteBuffer attachment) {

            }
        });


    }

    @Test
    public void testReadFileChannelFuture() throws IOException {
        //1.创建异步文件对象
        Path path = Paths.get("D:/nio/002.txt");
        AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        //2.创建buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //3.调用 channel 的 read方法得到future
        Future<Integer> future = asynchronousFileChannel.read(buffer, 0);
        //4.判断是否已经完成 isDone, 返回true
        while (!future.isDone());
            buffer.flip();

            byte [] data = new byte [buffer.limit()];
            buffer.get(data);
            System. out .println( new String(data));
            buffer.clear();
        }




}
